/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
 */

using System;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace Triax.Modules.Category
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditCategory class is used to manage content
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    partial class EditCategory : PortalModuleBase
    {

    #region Private Members

        private int CategoryId = Null.NullInteger;

    #endregion

    #region Event Handlers

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                //Determine CategoryId of Category to Update
                if(this.Request.QueryString["CategoryId"] !=null)
                {
                    CategoryId = Int32.Parse(this.Request.QueryString["CategoryId"]);
                }
                
                //If this is the first visit to the page, bind the role data to the datalist
                if (Page.IsPostBack == false)
                {
                    cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");

                    // Init dropdownlist                    
                    this.BindListStatus();
                    
                    if(CategoryId != -1)
                    {
                        //get content
                        CategoryController objCategorys = new CategoryController();
                        CategoryInfo objCategory = objCategorys.GetCategory(ModuleId,CategoryId);
                        if (objCategory != null)
                        {
                            txtCategoryName.Text = objCategory.CategoryName;                            
                            txtDescription.Text = objCategory.Description;
                            raListPublic.SelectedValue = objCategory.Status.ToString();
                            
                            ctlAudit.CreatedByUser = objCategory.CreatedByUser.ToString();
                            ctlAudit.CreatedDate = objCategory.CreatedDate.ToString();
                        }
                        else
                        {
                            Response.Redirect(Globals.NavigateURL(), true);
                        }
                    }
                    else
                    {
                        cmdDelete.Visible = false;
                        ctlAudit.Visible = false;
                    }
                }
           }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }


        
        // init radio Status
        protected void BindListStatus()
        {
            raListPublic.Items.Clear();
            raListPublic.DataSource = ModuleCategoryConst._DIC_STATUS;
            raListPublic.DataValueField = "Key";
            raListPublic.DataTextField = "Value";
            raListPublic.SelectedValue = ModuleCategoryConst._ON.ToString();
            raListPublic.DataBind();            
        }

        // function validate form
        protected bool IsValidForm()
        {
            lblError.Text = "";
            bool Flag = true;
            string MsgError = "";
            if (txtCategoryName.Text == "")
            {
                MsgError += Localization.GetString("ErrorEmpty_CategoryName", LocalResourceFile); ;
                Flag = false;
            }

            if (!Flag)
            {
                lblError.Text += "<ul>" + MsgError + "</ul>";
                return false;
            }
            return true;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdCancel_Click runs when the cancel button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdDelete_Click runs when the delete button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdDelete_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                //Only attempt to delete the item if it exists already
                if (!Null.IsNull(CategoryId))
                {
                    CategoryController objCategorys = new CategoryController();
                    objCategorys.DeleteCategory(ModuleId,CategoryId);
 
                    //refresh cache
                    SynchronizeModule();
                }

                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdUpdate_Click runs when the update button is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (this.IsValidForm())
                {
                    CategoryController objCategorys = new CategoryController();
                    CategoryInfo objCategory = new CategoryInfo();

                    objCategory.ModuleId = ModuleId;
                    objCategory.CategoryId = CategoryId;
                    objCategory.CategoryName = txtCategoryName.Text;
                    objCategory.CategoryType = 3;
                    objCategory.Description = txtDescription.Text;
                    objCategory.Status = int.Parse(raListPublic.SelectedValue.ToString());
                    
                    if (Null.IsNull(CategoryId))
                    {
                        objCategory.CreatedByUser = this.UserId;
                    }
                    objCategory.UpdatedByUser = this.UserId;

                    //Update the content within the Category table
                    if (Null.IsNull(CategoryId))
                    {
                        objCategorys.AddCategory(objCategory);
                    }
                    else
                    {
                        objCategorys.UpdateCategory(objCategory);
                    }

                    //refresh cache
                    SynchronizeModule();

                    //Redirect back to the portal home page
                    this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
                }
             }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

    #endregion

    }
}

